package com.jindy.module.leaseFinancialStatistics.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.utils.StringUtils;
import com.jindy.module.leaseFinancialStatistics.domain.TurnoverCountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jindy.module.leaseFinancialStatistics.mapper.LeaseFinancialStatisticsMapper;
import com.jindy.module.leaseFinancialStatistics.domain.LeaseFinancialStatistics;
import com.jindy.module.leaseFinancialStatistics.service.ILeaseFinancialStatisticsService;
import org.springframework.util.CollectionUtils;

import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * VIEWService业务层处理
 * 
 * @author jindy
 * @date 2023-06-06
 */
@Service
public class LeaseFinancialStatisticsServiceImpl implements ILeaseFinancialStatisticsService 
{
    @Autowired
    private LeaseFinancialStatisticsMapper leaseFinancialStatisticsMapper;

    /**
     * 查询租赁订单统计
     * 
     * @param leaseMonth 租赁订单统计主键
     * @return 租赁订单统计
     */
    @Override
    public LeaseFinancialStatistics selectLeaseFinancialStatisticsByLeaseMonth(String leaseMonth)
    {
        return leaseFinancialStatisticsMapper.selectLeaseFinancialStatisticsByLeaseMonth(leaseMonth);
    }

    /**
     * 查询租赁订单统计列表
     * 
     * @param leaseFinancialStatistics 租赁订单统计
     * @return 租赁订单统计
     */
    @Override
    public List<LeaseFinancialStatistics> selectLeaseFinancialStatisticsList(LeaseFinancialStatistics leaseFinancialStatistics)
    {
        return leaseFinancialStatisticsMapper.selectLeaseFinancialStatisticsList(leaseFinancialStatistics);
    }

    /**
     * 新增租赁订单统计
     * 
     * @param leaseFinancialStatistics 租赁订单统计
     * @return 结果
     */
    @Override
    public int insertLeaseFinancialStatistics(LeaseFinancialStatistics leaseFinancialStatistics)
    {
        return leaseFinancialStatisticsMapper.insertLeaseFinancialStatistics(leaseFinancialStatistics);
    }

    /**
     * 修改租赁订单统计
     * 
     * @param leaseFinancialStatistics 租赁订单统计
     * @return 结果
     */
    @Override
    public int updateLeaseFinancialStatistics(LeaseFinancialStatistics leaseFinancialStatistics)
    {
        return leaseFinancialStatisticsMapper.updateLeaseFinancialStatistics(leaseFinancialStatistics);
    }

    /**
     * 批量删除租赁订单统计
     * 
     * @param leaseMonths 需要删除的租赁订单统计主键
     * @return 结果
     */
    @Override
    public int deleteLeaseFinancialStatisticsByLeaseMonths(String[] leaseMonths)
    {
        return leaseFinancialStatisticsMapper.deleteLeaseFinancialStatisticsByLeaseMonths(leaseMonths);
    }

    /**
     * 删除租赁订单统计信息
     * 
     * @param leaseMonth 租赁订单统计主键
     * @return 结果
     */
    @Override
    public int deleteLeaseFinancialStatisticsByLeaseMonth(String leaseMonth)
    {
        return leaseFinancialStatisticsMapper.deleteLeaseFinancialStatisticsByLeaseMonth(leaseMonth);
    }

    /**
     * 租赁业务统计
     * @return
     */
    @Override
    public AjaxResult getTurnoverCount() {
        List<Map<String,Object>> list = leaseFinancialStatisticsMapper.getTurnoverCount();
        List<TurnoverCountVO> turnoverCountVoArrayList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)){
            return AjaxResult.success(turnoverCountVoArrayList);
        }
        //获取当前时间
        LocalDate nowDate = LocalDate.now();
        int year = nowDate.getYear();
        for (int i =1;i<=12;i++){
            TurnoverCountVO turnoverCountVO = new TurnoverCountVO();
            turnoverCountVO.setMonth(i+"月份");
            for (int startYear=2019;startYear<=year;startYear++){
                for (Map<String, Object> map : list) {
                    String leaseDateMonth = (String) map.get("leaseDateMonth");
                    //拼接日期
                    String yearStr=startYear+"";
                    String monthStr = String.format("%02d", i);
                    if ((yearStr+"-"+monthStr).equals(leaseDateMonth)){
                        if (startYear==2019){
                            BigDecimal payAmountSum= Objects.isNull(map.get("payAmountSum"))? BigDecimal.ZERO : (BigDecimal) map.get("payAmountSum");
                            turnoverCountVO.setNineteenTurnover(payAmountSum);
                        }
                        if (startYear==2020){
                            BigDecimal payAmountSum=Objects.isNull(map.get("payAmountSum"))? BigDecimal.ZERO : (BigDecimal) map.get("payAmountSum");
                            turnoverCountVO.setTwentyTurnover(payAmountSum);
                        }
                        if (startYear==2021){
                            BigDecimal payAmountSum=Objects.isNull(map.get("payAmountSum"))? BigDecimal.ZERO : (BigDecimal) map.get("payAmountSum");
                            turnoverCountVO.setTwentyOneTurnover(payAmountSum);
                        }
                        if (startYear==2022){
                            BigDecimal payAmountSum=Objects.isNull(map.get("payAmountSum"))? BigDecimal.ZERO : (BigDecimal) map.get("payAmountSum");
                            turnoverCountVO.setTwentyTwoTurnover(payAmountSum);
                        }
                        if (startYear==2023){
                            BigDecimal payAmountSum=Objects.isNull(map.get("payAmountSum"))? BigDecimal.ZERO : (BigDecimal) map.get("payAmountSum");
                            turnoverCountVO.setTwentyThreeTurnover(payAmountSum);
                        }
                    }
                }
            }
            turnoverCountVoArrayList.add(turnoverCountVO);
        }
        return AjaxResult.success(turnoverCountVoArrayList);
    }
}
